//
//  OrderSubListViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/9.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "OrderSubListCell.h"
#import "SCLAlertView.h"

#import "OrderSubListSectionFooterView.h"
#import "OrderSubListSectionHeaderView.h"

//#import "OrderDetailViewController.h"

#import "OrderSubListViewController.h"

@interface OrderSubListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableview;

@property (nonatomic, strong) NSMutableArray <OrderListSupplierModel *>*dataArray;

@property (nonatomic, strong) NSMutableArray *footerConfigArray;

//这个枚举实际对应的是分段的排列
@property (nonatomic, assign) EXTOrderType type;

//这个用来请求实际对应分段的数据
@property (nonatomic, assign) NSInteger requestType;

@property (nonatomic, assign) NSInteger pageSize;

@property (nonatomic, assign) NSInteger pageNum;

@end

@implementation OrderSubListViewController

- (instancetype)initWithType:(EXTOrderType)type {
    
    self = [super init];
    
    if (self) {
        self.type = type;
        
        switch (type) {
            case EXTOrderType_ALL:{
                
                self.requestType = 0;
            } break;
            case EXTOrderType_WaitPay:{
                
                self.requestType = 1;
            } break;
            case EXTOrderType_RecOrder:{
                self.requestType = 2;

            } break;
            case EXTOrderType_WaitService:{
                self.requestType = 3;

            } break;
            case EXTOrderType_Waitrefund:{
                self.requestType = 4;

            } break;
            case EXTOrderType_Finished:{
                self.requestType = 5;

            } break;
            default:
                break;
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self refreshParam];
    [self request_orderList];
}

- (void)refreshParam {
    
    self.pageSize = 20;
    self.pageNum = 1;
    self.dataArray = @[].mutableCopy;
}

- (void)initView {
    
    self.title = @"提交订单";

    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    
    [self.tableview reloadData];
}

- (void)request_orderList {
    
//    NSMutableDictionary *dic = @{}.mutableCopy;
////    [dic setValue:@"" forKey:@"keyword"];
//    [dic setValue:@(self.pageNum) forKey:@"pageNum"];
//    [dic setValue:@(self.pageSize) forKey:@"pageSize"];
//    [dic setValue:@(self.requestType) forKey:@"status"];
//
//    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_API_LIST_ORDER];
//    WEAKSELF
//
//    [SVProgressHUD showWithStatus:@"正在请求订单列表"];
//    [TTHttpManager getRequestWithUrl:url TimeOut:10.0f GetData:[TTHttpManager cacheBase64ParamWithDic:dic] FinishBlock:^(id  _Nonnull data) {
//
//        [SVProgressHUD dismiss];
//        [weakSelf.tableview.mj_header endRefreshing];
//        [weakSelf.tableview.mj_footer endRefreshing];
//        [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];
//
//        if ([data isKindOfClass:[NSDictionary class]]) {
//
//            //请求成功
//            NSInteger code = [[data valueForKey:@"code"] integerValue];
//            if (code == 200) {
//
//                NSDictionary *resultDic = [data valueForKey:@"data"];
//                NSDictionary *orderPage = [resultDic valueForKey:@"orderPage"];
//                NSArray *list = [orderPage valueForKey:@"list"];
//
//                for (int i = 0; i < list.count; i++) {
//                    NSDictionary *supplierDic = list[i];
//                    OrderListSupplierModel *model = [OrderListSupplierModel mj_objectWithKeyValues:supplierDic];
//
//                    model.goodsList = @[].mutableCopy;
//                    NSArray *goodsList = [supplierDic valueForKey:@"goodsList"];
//                    for (int j = 0; j < goodsList.count; j++) {
//                        NSDictionary *goodsDic = goodsList[j];
//                        OrderListGoodsModel *goodsModel = [OrderListGoodsModel mj_objectWithKeyValues:goodsDic];
//                        [model.goodsList addObject:goodsModel];
//                    }
//
//                    //底部按钮配置存model
//                    model.footerBtnList = @[].mutableCopy;
//                    for (NSDictionary *dic in self.footerConfigArray) {
//
//                        NSInteger footerShowType = [[dic valueForKey:@"footerShowType"] integerValue];
//                        if (footerShowType == model.orderStatus) {
//
//                            NSArray *btnArray = [dic valueForKey:@"btnArray"];
//                            for (NSDictionary *btnDic in btnArray) {
//                                OrderListFooterModel *footerBtnModel = [OrderListFooterModel mj_objectWithKeyValues:btnDic];
//                                [model.footerBtnList addObject:footerBtnModel];
//                            }
//                        }
//                    }
//
//                    [weakSelf.dataArray addObject:model];
//                }
//
//                if (list.count < weakSelf.pageSize) {
//                    [weakSelf.tableview.mj_footer endRefreshingWithNoMoreData];
//                }
//
//                if (weakSelf.pageNum == 1 && weakSelf.dataArray.count == 0) {
//
//                    [[TTNoResultManager shareManager] showNoResultWithStateInView:self.tableview errorCode:kTTViewStateNoData btnTitle:@"重新加载" didPressButton:^{
//                        [weakSelf request_orderList];
//                    }];
//                } else {
//
//                    [weakSelf.tableview reloadData];
//                }
//
//            } else {
//                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
//                [SVProgressHUD dismissWithDelay:2];
//            }
//        }
//
//    } FailedBlock:^(NSError * _Nonnull error) {
//        [SVProgressHUD dismiss];
//        [weakSelf.tableview.mj_header endRefreshing];
//        [weakSelf.tableview.mj_footer endRefreshing];
//        WEAKSELF
//        [[TTNoResultManager shareManager] showNoResultWithStateInView:self.tableview errorCode:error.code btnTitle:@"重新加载" didPressButton:^{
//            [weakSelf request_orderList];
//        }];
//
//    }];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return self.dataArray.count;
    
    return 10;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return self.dataArray[section].goodsList.count;
    return 1;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 80;
}

#pragma mark sectionheader
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    OrderSubListSectionHeaderView *headView = [[NSBundle mainBundle] loadNibNamed:@"OrderSubListSectionHeaderView"owner:nil options:nil].firstObject;
    if (self.dataArray.count > section) {
        [headView fillCellWithModel:self.dataArray[section]];
    }
    return  headView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    OrderSubListSectionFooterView *footerView = [[NSBundle mainBundle] loadNibNamed:@"OrderSubListSectionFooterView"owner:nil options:nil].firstObject;
//    [footerView fillFooterWithModel:self.dataArray[section]];
    WEAKSELF
    footerView.OrderSubListSectionFooterBlock = ^(OrderSubListSectionFooterViewAction action) {
    
//        switch (action) {
//            case OrderSubListSectionFooterViewAction_payOrder:{
//
//                OrderPayViewController *vc = [[OrderPayViewController alloc] init];
//                vc.totalPrice = self.dataArray[section].orderPrice;
//                vc.orderId = self.dataArray[section].orderId;
//                [weakSelf.navigationController pushViewController:vc animated:YES];
//
//            } break;
//            case OrderSubListSectionFooterViewAction_cancleOrder:{
//
//                [weakSelf request_cancleOrderWithOrderId:weakSelf.dataArray[section].orderId];
//
//            } break;
//            case OrderSubListSectionFooterViewAction_applyAfterSale:{
//
//                [weakSelf alertEditWithBlock:^(NSString *content) {
//
//                    [weakSelf request_applyAfterSaleWithOrderId:weakSelf.dataArray[section].orderId AndReason:content];
//
//                } AndSesc:@"填写申请售后原因"];
//
//
//            } break;
//            case OrderSubListSectionFooterViewAction_remindSend:{
//
//                [weakSelf request_remindOrderSendWithOrderId:weakSelf.dataArray[section].orderId];
//
//            } break;
//            case OrderSubListSectionFooterViewAction_cancleAfterSale:{
//
//                [weakSelf request_cancleAfterSaleWithOrderId:weakSelf.dataArray[section].orderId];
//
//            } break;
//            case OrderSubListSectionFooterViewAction_sureReceive:{
//
//                [weakSelf request_sureReceiveWithOrderId:weakSelf.dataArray[section].orderId];
//
//            } break;
//
//
//            default:
//                break;
//        }
    };
    
    return  footerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 86;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

//    OrderDetailViewController *vc = [[OrderDetailViewController alloc] init];
//    vc.orderId = self.dataArray[indexPath.section].orderId;
//    [self.navigationController pushViewController:vc animated:YES];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    OrderSubListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderSubListCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
//    [cell fillOrderListCellWithModel:self.dataArray[indexPath.section].goodsList[indexPath.row]];
    
    return cell;
}


- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"OrderSubListCell" bundle:nil] forCellReuseIdentifier:@"OrderSubListCellID"];
        
        WEAKSELF
        _tableview.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            
            [weakSelf refreshParam];
            [weakSelf request_orderList];
        }];
        
        _tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            
            weakSelf.pageNum ++;
            [weakSelf request_orderList];

        }];
    }
    return _tableview;
}

- (NSMutableArray<OrderListSupplierModel *> *)dataArray {
    
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}


- (NSMutableArray *)footerConfigArray {
    /*
     订单状态: 1待支付 2待发货 3已发货 4已完成 5已取消 6退款申请 7退款完成
     footerShowType 订单状态
     btnArray 按钮配置
      btnTitle按钮标题
      btnAction按钮行为 对应 OrderSubListSectionFooterViewAction枚举
      btnColor按钮颜色 0黑色 1红色
     订单状态: 1待支付 2待发货 3已发货 4已完成 5已取消 6退款申请 7退款完成
     */
    if (!_footerConfigArray) {
        _footerConfigArray = @[].mutableCopy;
        _footerConfigArray = @[
            @{@"footerShowType": @(1),
              @"btnArray":@[@{@"btnTitle":@"付款",@"btnAction":@(0),@"btnColorType":@(1)},
                            @{@"btnTitle":@"取消订单",@"btnAction":@(1),@"btnColorType":@(0)}],
            },
            
            @{@"footerShowType": @(2),
              @"btnArray":@[@{@"btnTitle":@"提醒发货",@"btnAction":@(3),@"btnColorType":@(1)},
                          @{@"btnTitle":@"申请退款",@"btnAction":@(2),@"btnColorType":@(0)}]
            },
            
            @{@"footerShowType": @(3),
              @"btnArray":@[@{@"btnTitle":@"申请退款",@"btnAction":@(2),@"btnColorType":@(1)},
                          @{@"btnTitle":@"确认收货",@"btnAction":@(5),@"btnColorType":@(0)}]
            },
            
            @{@"footerShowType": @(4),
              @"btnArray":@[]
            },
            
            @{@"footerShowType": @(6),
              @"btnArray":@[@{@"btnTitle":@"取消退款",@"btnAction":@(4),@"btnColorType":@(1)}]
            },
        ].mutableCopy;
    }

    return _footerConfigArray;
}

@end
